package cc.vv.baselibrary.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

import cc.vv.baselibrary.R;
import cc.vv.baselibrary.util.ColorUtil;
import cc.vv.baselibrary.util.LKCommonUtil;
import cc.vv.baselibrary.util.LKLogUtil;


/**
 * 自动换行textView控件
 *
 * @author sun.li
 */
public class BCFeedView extends LinearLayout {

    private LayoutInflater layoutInflater;
    /**
     * 上下文
     */
    private Context mContext;

    private LinearLayout ll_linefeedRoot;

    private View mView;

    /**
     * 布局内控件点击事件回调对象
     */
    private TVOnClick tvOnClick;

    /**
     * 选中的新闻栏目信息数据
     */
    private ArrayList<String> selectedNewsChannelInfoData;

    /**
     * 选中的String类型Item数据
     */
    private ArrayList<String> selectedStringData;

    /**
     * 默认第一个key
     */
    private String firstKey = "1";
    /**
     * 默认第二个key
     */
    private String secondKey = "2";
    /**
     * 默认第三个key
     */
    private String thirdKey = "3";

    /**
     * 是否是点击
     */
    private boolean isClick = false;

    public BCFeedView(Context context) {
        this(context, null);
    }

    public BCFeedView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BCFeedView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initView();
    }

    private void initView() {
        layoutInflater = LayoutInflater.from(mContext);
        mView = layoutInflater.inflate(R.layout.layout_linefeed_ltextview, null);
        ll_linefeedRoot = mView.findViewById(R.id.ll_linefeedRoot);
        this.addView(mView);
        selectedNewsChannelInfoData = new ArrayList<>();
        selectedStringData = new ArrayList<>();
    }

    /**
     * 获取选中的新闻栏目信息数据
     */
    public ArrayList<String> getSelectedNewsChannelInfoData() {
        return selectedNewsChannelInfoData;
    }


    /**
     * 获取选中的String类型Item数据
     */
    public ArrayList<String> getSelectedStringData() {
        return selectedStringData;
    }

    public TVOnClick getTvOnClick() {
        return tvOnClick;
    }

    /**
     * 设置布局内控件点击事件回调对象
     */
    public void setTvOnClick(TVOnClick tvOnClick) {
        this.tvOnClick = tvOnClick;
    }

    /**
     * 设置NewsChannelInfo类型数据
     *
     * @param tags
     */
    public void initNewsChannelData(final ArrayList<String> tags) {
        if (tags == null || tags.size() <= 0) {
            return;
        }
        ll_linefeedRoot.removeAllViews();
        int screenWidth = LKCommonUtil.getScreenWidth() - LKCommonUtil.dip2px(20);
        ll_linefeedRoot.setOrientation(LinearLayout.VERTICAL);
        LinearLayout linearLayout = new LinearLayout(mContext);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        ArrayList<View> textViews = new ArrayList<>();
        int width;
        for (int i = 0; i < tags.size(); i++) {
            final int index = i;
            width = 0;
            for (int j = 0; j < textViews.size(); j++) {
                View textView_1 = textViews.get(j);
                textView_1.measure(0, 0);
                width += textView_1.getMeasuredWidth();
            }

            String text = tags.get(i);
            View view = null;
            try {
                view = layoutInflater.inflate(R.layout.view_selected_channel_item, null);
            } catch (Exception e) {
                LKLogUtil.e(e.getMessage(), e);
                view = null;
            }
            if (view == null) {
                continue;
            }
            TextView textView = null;
            try {
                textView = view.findViewById(R.id.tv_vsci_content);
            } catch (Exception e) {
                LKLogUtil.e(e.getMessage(), e);
                textView = new TextView(mContext);
            }
            if ("0".equals(tags.get(i))) {
                textView.setBackground(mContext.getResources().getDrawable(R.drawable.line_feed_item_uncheck_shape));
                textView.setTextColor(ColorUtil.setBCColor(R.color.color_FFFFFF));
                selectedNewsChannelInfoData.add(tags.get(i));
            } else {
                textView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        try {
                            TextView tv = (TextView) view;
                            if (tv.getCurrentTextColor() == ColorUtil.setBCColor(R.color.color_2D2A2A)) {
                                tv.setBackground(mContext.getResources().getDrawable(R.drawable.line_feed_item_uncheck_shape));
                                tv.setTextColor(ColorUtil.setBCColor(R.color.color_FFFFFF));
                                selectedNewsChannelInfoData.add(tags.get(index));
                            } else {
                                tv.setBackground(mContext.getResources().getDrawable(R.drawable.line_feed_item_selected_shape));
                                tv.setTextColor(ColorUtil.setBCColor(R.color.color_2D2A2A));
                                selectedNewsChannelInfoData.remove(tags.get(index));
                            }
                        } catch (Exception e) {
                            LKLogUtil.e(e.getMessage(), e);
                        }
                    }
                });
            }
            textView.setText(text);

            textView.measure(0, 0);

            view.measure(0, 0);
            linearLayout.setGravity(Gravity.CENTER);
            if ((width + view.getMeasuredWidth()) > screenWidth) {
                ll_linefeedRoot.addView(linearLayout);
                textViews.clear();
                textViews.add(view);
                linearLayout = new LinearLayout(mContext);
                linearLayout.setOrientation(LinearLayout.HORIZONTAL);
                linearLayout.addView(view);
            } else {
                textViews.add(view);
                linearLayout.addView(view);
            }
            if (i == tags.size() - 1) {
                ll_linefeedRoot.addView(linearLayout);
            }
        }
    }


    /**
     * 设置String类型数据
     *
     * @param tags
     */
    public void initStringData(final ArrayList<String> tags, boolean textClick) {
        if (tags == null || tags.size() <= 0) {
            return;
        }
        this.isClick = textClick;
        int count = tags.size();
//        if (count > 8) {
//            count = 8;
//        }
        ll_linefeedRoot.removeAllViews();
        int screenWidth = LKCommonUtil.getScreenWidth() - LKCommonUtil.dip2px(20);
        ll_linefeedRoot.setOrientation(LinearLayout.VERTICAL);
        LinearLayout linearLayout = new LinearLayout(mContext);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        ArrayList<View> textViews = new ArrayList<>();
        int width;
        for (int i = 0; i < count; i++) {
            final int index = i;
            width = 0;
            for (int j = 0; j < textViews.size(); j++) {
                View textView_1 = textViews.get(j);
                textView_1.measure(0, 0);
                width += textView_1.getMeasuredWidth();
            }
            String text = tags.get(i);
            View view;
            try {
                view = layoutInflater.inflate(R.layout.view_selected_string_item, null);
            } catch (Exception e) {
                LKLogUtil.e(e.getMessage(), e);
                view = null;
            }
            if (view == null) {
                continue;
            }
            TextView textView;
            try {
                textView = view.findViewById(R.id.tv_vssi_content);
                if (isClick) {
                    textView.setBackground(mContext.getResources().getDrawable(R.drawable.line_feed_item_selected_click_selector));

                }
            } catch (Exception e) {
                LKLogUtil.e(e.getMessage(), e);
                textView = new TextView(mContext);
            }
            textView.setText(text);
            textView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (getTvOnClick() == null) {
                        if (isClick) {
                            //点击item
                            if (null != getTvOnClick()) {
                                getTvOnClick().onStringTVClick(tags.get(index));
                            }
                        } else {
                            //选中item
                            try {
                                TextView tv = (TextView) view;
                                if (tv.getCurrentTextColor() == ColorUtil.setBCColor(R.color.color_2D2A2A)) {
                                    tv.setBackground(mContext.getResources().getDrawable(R.drawable.line_feed_item_uncheck_shape));
                                    tv.setTextColor(ColorUtil.setBCColor(R.color.color_FFFFFF));
                                    selectedStringData.add(tags.get(index));
                                } else {
                                    tv.setBackground(mContext.getResources().getDrawable(R.drawable.line_feed_item_selected_shape));
                                    tv.setTextColor(ColorUtil.setBCColor(R.color.color_2D2A2A));
                                    selectedStringData.remove(tags.get(index));
                                }
                            } catch (Exception e) {
                                LKLogUtil.e(e.getMessage(), e);
                            }
                        }
                    } else {
                        if (null != getTvOnClick()) {
                            getTvOnClick().onStringTVClick(tags.get(index));
                        }
                    }
                }
            });
            textView.measure(0, 0);

            view.measure(0, 0);
            linearLayout.setGravity(Gravity.LEFT);
            if ((width + view.getMeasuredWidth()) > screenWidth) {
                ll_linefeedRoot.addView(linearLayout);
                textViews.clear();
                textViews.add(view);
                linearLayout = new LinearLayout(mContext);
                linearLayout.setOrientation(LinearLayout.HORIZONTAL);
                linearLayout.addView(view);
            } else {
                textViews.add(view);
                linearLayout.addView(view);
            }
            if (i == count - 1) {
                ll_linefeedRoot.addView(linearLayout);
            }
        }
    }

    /**
     * 布局内控件点击事件回调
     */
    public interface TVOnClick {

        /**
         * Item点击回调
         *
         * @param text TextView内容
         */
        void onStringTVClick(String text);
    }
}
